<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Postage as PostageModel;
use app\yunda\model\China;
use think\Db;

/**
 * Class Category运费设置
 * @package app\yunda\admin
 */
class Postage extends Base{
    public function index($config_type='postage')
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $map['delete_time']=NULL;
        $data = Db::table('dp_yd_postage')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['sp']=China::getPlace($item['sp'])['name'];
                $item['gp']=China::getPlace($item['gp'])['name'];
                return $item;
            });
        $list_tab=$this->szMenu();
        return ZBuilder::make('table')
            ->setTabNav($list_tab,  'postage')
            ->setTableName('yd_postage')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['sp','寄件省份'],
                ['gp','收件省份'],
                ['head_weight','寄件首重(Kg)'],
                ['hw_money','寄件首重金额(￥)'],
                ['ow_money','寄件续重金额（￥/kg）'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['sp'],China::getProvince(1,'admin',1))
            ->addFilter(['gp'],China::getProvince(1,'admin',1))
            //->setSearch(['id' => 'ID', 'config_name' => '连续签到天数'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Postage');
            if($result!==true) $this->error($result);
            // 第三部 存储
            PostageModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
//                ['linkage:4','sp','寄件省份','请选择省',China::getProvince(),'',url('get_place'),'sc,sa'],
//                ['linkage:4','sc','寄件城市','请选择省','','',url('get_place'),'sa'],
//                ['select:4','sa','寄件地区','请选择区'],
//                ['linkage:4','gp','收件省份','请选择省',China::getProvince(),'',url('get_place'),'gc,ga'],
//                ['linkage:4','gc','收件城市','请选择省','','',url('get_place'),'ga'],
//                ['select:4','ga','收件地区','请选择区'],
                ['select:4','sp','寄件省份','请选择省',China::getProvince()],
                ['select:4','gp','收件省份','请选择省',China::getProvince()],
                ['number','head_weight','寄件首重(Kg)'],
                ['number','hw_money','寄件首重金额(￥)'],
                ['number','ow_money','寄件续重金额（￥/kg）'],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Postage');
            if($result!==true) $this->error($result);
            if($id==5)$data['config_name']=1;
            PostageModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = PostageModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
//                ['linkage:4','sp','寄件省份','请选择省',China::getProvince(),'',url('get_place'),'sc,sa'],
//                ['linkage:4','sc','寄件城市','请选择省',China::getProvince($data['sp']),'',url('get_place'),'sa'],
//                ['select:4','sa','寄件地区','请选择区',China::getProvince($data['sc'])],
//                ['linkage:4','gp','收件省份','请选择省',China::getProvince(),'',url('get_place'),'gc,ga'],
//                ['linkage:4','gc','收件城市','请选择省',China::getProvince($data['gp']),'',url('get_place'),'ga'],
//                ['select:4','ga','收件地区','请选择区',China::getProvince($data['gc'])],
                ['select:4','sp','寄件省份','请选择省',China::getProvince()],
                ['select:4','gp','收件省份','请选择省',China::getProvince()],
                ['number','head_weight','寄件首重(Kg)'],
                ['number','hw_money','寄件首重金额(￥)'],
                ['number','ow_money','寄件续重金额（￥/kg）'],
            ])
            ->setFormData($data)
            ->fetch();
    }
    //软删除
    public function delete($record = [])
    {
        $data=request()->param();
        if(in_array(5,$data['ids']))$this->error('不能删除默认签到配置');
        PostageModel::destroy(['id'=>['in',$data['ids']]]);//删除多条
        $this->success('删除成功', null, '_parent_reload');
    }

    //获取联动菜单数据
    public function get_place(){
        $id=request()->param();
        $id=implode(",",$id);
        $arr['code']='1';
        $arr['msg']='请求成功';
        $arr['list']=format_linkage(Db::table('dp_yd_region')->where('pid',$id)->column('id,name'));
        return $arr;
    }
}